<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>7. Bedingungen &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <script type="text/javascript" src="_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="index.html" />
    <link rel="next" title="8. Schleifen" href="powershell_schleifen.html" />
    <link rel="prev" title="6. Arrays und Hashs" href="powershell_arrays.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li class="right" >
          <a href="powershell_schleifen.html" title="8. Schleifen"
             accesskey="N">weiter</a> |</li>
        <li class="right" >
          <a href="powershell_arrays.html" title="6. Arrays und Hashs"
             accesskey="P">zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">7. Bedingungen</a><ul>
<li><a class="reference internal" href="#einfache-vergleiche">7.1. Einfache Vergleiche</a></li>
<li><a class="reference internal" href="#umgekehrte-vergleiche">7.2. Umgekehrte Vergleiche</a></li>
<li><a class="reference internal" href="#verbinden-mehrerer-vergleiche">7.3. Verbinden mehrerer Vergleiche</a></li>
<li><a class="reference internal" href="#verzweigung">7.4. Verzweigung</a></li>
<li><a class="reference internal" href="#auswahl">7.5. Auswahl</a><ul>
<li><a class="reference internal" href="#default">7.5.1. Default</a></li>
<li><a class="reference internal" href="#break">7.5.2. Break</a></li>
<li><a class="reference internal" href="#string-vergleiche">7.5.3. String-Vergleiche</a></li>
<li><a class="reference internal" href="#regulare-ausdrucke">7.5.4. Reguläre Ausdrücke</a></li>
<li><a class="reference internal" href="#switch-auf-listen">7.5.5. switch auf Listen</a></li>
<li><a class="reference internal" href="#zusammenfassung">7.5.6. Zusammenfassung</a></li>
</ul>
</li>
<li><a class="reference internal" href="#aufgaben">7.6. Aufgaben</a><ul>
<li><a class="reference internal" href="#zahlenraten">7.6.1. <strong>ZahlenRaten</strong></a></li>
<li><a class="reference internal" href="#dateien-kopieren">7.6.2. <strong>Dateien kopieren</strong></a></li>
</ul>
</li>
<li><a class="reference internal" href="#losungen">7.7. Lösungen</a><ul>
<li><a class="reference internal" href="#id1">7.7.1. <strong>ZahlenRaten</strong></a></li>
<li><a class="reference internal" href="#id2">7.7.2. <strong>Dateien kopieren</strong></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Vorheriges Thema</h4>
  <p class="topless"><a href="powershell_arrays.html"
                        title="vorheriges Kapitel">6. Arrays und Hashs</a></p>
  <h4>Nächstes Thema</h4>
  <p class="topless"><a href="powershell_schleifen.html"
                        title="nächstes Kapitel">8. Schleifen</a></p>
  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/powershell_bedingung.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="bedingungen">
<h1>7. Bedingungen<a class="headerlink" href="#bedingungen" title="Permalink zu dieser Überschrift">¶</a></h1>
<p>Häufig wird man nicht einen <em>linearen</em> Quellcode schreiben, sondern in Abhängigkeit von bestimmten Bedingungen den einen oder den anderen Quellcode ausführen. Powershell kennt mehrere M?glichkeiten f?r das Pr?fen von Bedingungen.</p>
<div class="section" id="einfache-vergleiche">
<h2>7.1. Einfache Vergleiche<a class="headerlink" href="#einfache-vergleiche" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Mit Hilfe der Vergleichsoperatoren lässt sich eine Bedingung bereits in der Konsole vornehmen.</p>
<p>Im Gegensatz zu anderen Programmiersprachen verwendet PS bei der Formulierung von Vergleichsoperatoren eine der englischen Sprache verwandte Syntax und nicht die sonst verwendeten mathematischen Operationszeichen. Eine Übersicht finden sie hier <a class="reference internal" href="powershell_anhang_1.html#vergleichsoperator"><em>Vergleichsoperatoren</em></a>.</p>
<p id="index-0">Wie der Tabelle zu entnehmen ist, gibt es drei Varianten der Vergleichsoperatoren. Soll die Groß- und Kleinschreibung berücksichtigt werden, stellt man dem Operator ein &#8220;c&#8221;, ansonsten ein &#8220;i&#8221; voraus. Wird dies weggelassen, ist case-insensitive eingestellt.</p>
<p>Beurteilen Sie die Ergebnisse der folgenden Vergleiche:</p>
<div class="highlight-sh"><div class="highlight"><pre>4 -eq 10
<span class="s2">&quot;secret&quot;</span> -ieq <span class="s2">&quot;SECRET&quot;</span>
123 -lt 123.5
12 -eq <span class="s2">&quot;Hello&quot;</span>
12 -eq <span class="s2">&quot;000012&quot;</span>
<span class="s2">&quot;12&quot;</span> -eq 12
<span class="s2">&quot;12&quot;</span> -eq 012
<span class="s2">&quot;012&quot;</span> -eq 012
123 -lt 123.4
123 -lt <span class="s2">&quot;123.4&quot;</span>
123 -lt <span class="s2">&quot;123.5&quot;</span>
</pre></div>
</div>
<img alt="_images/vergleichs_operator_ergebnis.png" src="_images/vergleichs_operator_ergebnis.png" />
</div>
<div class="section" id="umgekehrte-vergleiche">
<h2>7.2. Umgekehrte Vergleiche<a class="headerlink" href="#umgekehrte-vergleiche" title="Permalink zu dieser Überschrift">¶</a></h2>
<p id="index-1">Ein Vergleich hat als Ergebnis immer entweder ein <em>true</em> oder ein <em>false</em>
Mit Hilfe des <em>-not</em> - Operators bzw. des <em>!</em> können diese Ergebnisse auch umgedreht werden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$a</span> <span class="o">=</span> 10
<span class="nv">$a</span> -gt 5
True
-not <span class="o">(</span><span class="nv">$a</span> -gt 5<span class="o">)</span>
False
<span class="c"># Shorthand: instead of -not &quot;!&quot; can also be used:</span>
!<span class="o">(</span><span class="nv">$a</span> -gt 5<span class="o">)</span>
False
</pre></div>
</div>
</div>
<div class="section" id="verbinden-mehrerer-vergleiche">
<h2>7.3. Verbinden mehrerer Vergleiche<a class="headerlink" href="#verbinden-mehrerer-vergleiche" title="Permalink zu dieser Überschrift">¶</a></h2>
<p id="index-2">Mit Hilfe der Verknüpfungsoperatoren <em>-and</em> bzw. <em>-or</em> können mehrere Vergleiche auch zu einem Gesamtvergleich verbunden werden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="o">(</span> <span class="o">(</span><span class="nv">$age</span> -ge 18<span class="o">)</span> -and <span class="o">(</span><span class="nv">$sex</span> -eq <span class="s2">&quot;m&quot;</span><span class="o">)</span> <span class="o">)</span>
</pre></div>
</div>
<p>:ref:: <cite>logischeOperatoren</cite></p>
<p>:ref:: <cite>auswahl_if_elseif_else</cite></p>
</div>
<div class="section" id="verzweigung">
<h2>7.4. Verzweigung<a class="headerlink" href="#verzweigung" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Die Vergleichsoperatoren geben zwar einen Hinweis auf True oder False, aber sie allein lassen keine Auswahlentscheidung des Programmiers zu. Deshalb finden die Vergleichsoperatoren ihren Einsatz innerhalb von Wenn-Dann-Statements, die eine Reaktion auf True oder False zulassen. Die Syntax dieser Anweisung ist wie folgt</p>
<div class="highlight-sh"><div class="highlight"><pre>If <span class="o">(</span>condition<span class="o">)</span> <span class="o">{</span>
<span class="c"># If the condition applies,</span>
<span class="c"># this code will be executed</span>
<span class="o">}</span>

<span class="nv">$a</span> <span class="o">=</span> 11
If <span class="o">(</span><span class="nv">$a</span> -gt 10<span class="o">)</span> <span class="o">{</span> <span class="s2">&quot;$a is larger than 10&quot;</span> <span class="o">}</span>
11 is larger than 10
</pre></div>
</div>
<img alt="_images/ps_ifelse.png" src="_images/ps_ifelse.png" />
<div class="highlight-sh"><div class="highlight"><pre>If <span class="o">(</span><span class="nv">$a</span> -gt 10<span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;$a is larger than 10&quot;</span>
<span class="o">}</span>
Else
<span class="o">{</span>
        <span class="s2">&quot;$a is less than or equal to 10&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Mehrere Bedingungen können über das <em>ElseIf</em> - Statement beschrieben werden.</p>
<div class="highlight-sh"><div class="highlight"><pre>If <span class="o">(</span><span class="nv">$a</span> -gt 10<span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;$a is larger than 10&quot;</span>
<span class="o">}</span>
Else
<span class="o">{</span>
        <span class="s2">&quot;$a is less than or equal to 10&quot;</span>
<span class="o">}</span>



If <span class="o">(</span><span class="nv">$a</span> -gt 10<span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;$a is larger than 10&quot;</span>
<span class="o">}</span>
ElseIf <span class="o">(</span><span class="nv">$a</span> -eq 10<span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;$a is exactly 10&quot;</span>
<span class="o">}</span>
ElseIf <span class="o">(</span><span class="nv">$a</span> -ge 10<span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;$a is larger than or equal to 10&quot;</span>
<span class="o">}</span>
Else
<span class="o">{</span>
        <span class="s2">&quot;$a is smaller than 10&quot;</span>
<span class="o">}</span>
</pre></div>
</div>
<p>Die Bedingungen können natürlich noch in sich verschachtelt werden</p>
<a class="reference internal image-reference" href="_images/ps_if_elseif.png"><img alt="_images/ps_if_elseif.png" src="_images/ps_if_elseif.png" style="width: 500px;" /></a>
</div>
<div class="section" id="auswahl">
<h2>7.5. Auswahl<a class="headerlink" href="#auswahl" title="Permalink zu dieser Überschrift">¶</a></h2>
<p id="index-3">UM eine Variable auf verschiedene Werte hin abzuprüfen, ist neben dem if-elseif-Statement noch eine weitere Möglichkeit vorhanden. Sie kann allerdings nur auf <strong>eine Variable</strong> angewendet werden.</p>
<a class="reference internal image-reference" href="_images/ps_switch.png"><img alt="_images/ps_switch.png" src="_images/ps_switch.png" style="width: 500px;" /></a>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$value</span> <span class="o">=</span> 1
<span class="nv">$switch</span><span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
        1 <span class="o">{</span><span class="s2">&quot;Number 1&quot;</span> <span class="o">}</span>
        2 <span class="o">{</span><span class="s2">&quot;Number 2&quot;</span> <span class="o">}</span>
        3 <span class="o">{</span><span class="s2">&quot;Number 3&quot;</span> <span class="o">}</span>
<span class="o">}</span>

Number 1
</pre></div>
</div>
<p>Als Standard-Vergelcihsoperator wird <em>-eq</em> genommen. Es sind aber auch andere Vergleiche denkbar.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$value</span> <span class="o">=</span> 8
Switch <span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
<span class="c"># Instead of a standard value, a code block is used</span>
<span class="c"># that results in True for numbers smaller than 5:</span>
<span class="o">{</span><span class="nv">$__</span>-le 5<span class="o">}</span> <span class="o">{</span> <span class="s2">&quot;Number from 1 to 5&quot;</span> <span class="o">}</span>
<span class="c"># A value is used here; Switch checks whether this</span>
<span class="c"># value matches $value:</span>
6 <span class="o">{</span> <span class="s2">&quot;Number 6&quot;</span> <span class="o">}</span>
<span class="c"># Complex conditions are allowed as they are here,</span>
<span class="c"># where -and is used to combine two comparisons:</span>
<span class="o">{((</span><span class="nv">$__</span>-gt 6<span class="o">)</span> -and <span class="o">(</span><span class="nv">$__</span>-le 10<span class="o">))}</span> <span class="o">{</span> <span class="s2">&quot;Number from 7 to 10&quot;</span> <span class="o">}</span>
<span class="o">}</span>
Number from 7 to 10
</pre></div>
</div>
<p>Auch ein Mischen ist möglich:</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$value</span> <span class="o">=</span> 8
Switch <span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
        <span class="c"># The initial value (here it is in $value)</span>
        <span class="c"># is available in the variable $_:</span>

        6 <span class="o">{</span> <span class="s2">&quot;Number 6&quot;</span> <span class="o">}</span>
        <span class="o">{((</span><span class="nv">$_</span> -gt 6<span class="o">)</span> -and <span class="o">(</span><span class="nv">$_</span> -le 10<span class="o">))}</span>
                <span class="o">{</span> <span class="s2">&quot;$_ is a number from 7 to 10&quot;</span> <span class="o">}</span>
<span class="o">}</span>
8 is a number from 7 to 10
</pre></div>
</div>
<div class="section" id="default">
<h3>7.5.1. Default<a class="headerlink" href="#default" title="Permalink zu dieser Überschrift">¶</a></h3>
<p id="index-4">Falls keine der Bedingungen zutrifft, kann mit Hilfe des Schlüsselwortes <strong>default</strong> eine Klausel ausgeführt werden.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$value</span> <span class="o">=</span> 50
Switch <span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
        <span class="o">{</span><span class="nv">$_</span> -le 5<span class="o">}</span> <span class="o">{</span> <span class="s2">&quot;$_is a number from 1 to 5&quot;</span> <span class="o">}</span>
        6 <span class="o">{</span> <span class="s2">&quot;Number 6&quot;</span> <span class="o">}</span>
        <span class="o">{((</span><span class="nv">$_</span> -gt 6<span class="o">)</span> -and <span class="o">(</span><span class="nv">$_</span> -le 10<span class="o">))}</span> <span class="o">{</span> <span class="s2">&quot;$_ is a number from 7 to 10&quot;</span> <span class="o">}</span>
        <span class="c"># The code after the next statement will be</span>
        <span class="c"># executed if no other condition has been met:</span>
        default <span class="o">{</span><span class="s2">&quot;$_ is a number outside the range from 1 to 10&quot;</span> <span class="o">}</span>
<span class="o">}</span>

50 is a number outside the range from 1 to 10
</pre></div>
</div>
</div>
<div class="section" id="break">
<h3>7.5.2. Break<a class="headerlink" href="#break" title="Permalink zu dieser Überschrift">¶</a></h3>
<p id="index-5">Häufig kann es vorkommen, dass mehrere Vergleiche innerhalb einer switch-Anweisung gültig sind. <strong>Powershell</strong> würde dann mehrere Klauseln ausführen. Um dies zu verhindern, kann man jede Klausel mit Hilfe des Schlüsselwortes <strong>break</strong> versehen. Nach der ersten erfolgreichen Klausel beendet <strong>Powershell</strong> die Ausführung.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$value</span> <span class="o">=</span> 50
Switch <span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
        50 <span class="o">{</span> <span class="s2">&quot;the number 50&quot;</span> <span class="o">}</span>
        <span class="o">{</span><span class="nv">$_</span> -gt 10<span class="o">}</span> <span class="o">{</span><span class="s2">&quot;larger than 10&quot;</span><span class="o">}</span>
        <span class="o">{</span><span class="nv">$_</span> -is <span class="o">[</span>int<span class="o">]}</span> <span class="o">{</span><span class="s2">&quot;Integer number&quot;</span><span class="o">}</span>
<span class="o">}</span>

The Number 50
Larger than 10
Integer number

<span class="nv">$value</span> <span class="o">=</span> 50
Switch <span class="o">(</span><span class="nv">$value</span><span class="o">)</span>
<span class="o">{</span>
        50 <span class="o">{</span> <span class="s2">&quot;the number 50&quot;</span>; <span class="nb">break</span> <span class="o">}</span>
        <span class="o">{</span><span class="nv">$_</span> -gt 10<span class="o">}</span> <span class="o">{</span><span class="s2">&quot;larger than 10&quot;</span>; <span class="nb">break</span><span class="o">}</span>
        <span class="o">{</span><span class="nv">$_</span> -is <span class="o">[</span>int<span class="o">]}</span> <span class="o">{</span><span class="s2">&quot;Integer number&quot;</span>; <span class="nb">break</span><span class="o">}</span>
<span class="o">}</span>
The number 50
</pre></div>
</div>
</div>
<div class="section" id="string-vergleiche">
<h3>7.5.3. String-Vergleiche<a class="headerlink" href="#string-vergleiche" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Da switch nur auf Gleichheit prüft, sind auch andere Datentypen vergleichbar.</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$action</span> <span class="o">=</span> <span class="s2">&quot;sAVe&quot;</span>
Switch <span class="o">(</span><span class="nv">$action</span><span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;save&quot;</span> <span class="o">{</span> <span class="s2">&quot;I save...&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;open&quot;</span> <span class="o">{</span> <span class="s2">&quot;I open...&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;print&quot;</span><span class="o">{</span> <span class="s2">&quot;I print...&quot;</span> <span class="o">}</span>
        Default<span class="o">{</span> <span class="s2">&quot;Unknown command&quot;</span> <span class="o">}</span>
<span class="o">}</span>
I save...

Groß- und Kleinschreibung kann man mit der Option -case eingeschaltet werden
</pre></div>
</div>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$action</span> <span class="o">=</span> <span class="s2">&quot;sAVe&quot;</span>
Switch -case <span class="o">(</span><span class="nv">$action</span><span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;save&quot;</span> <span class="o">{</span> <span class="s2">&quot;I save...&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;open&quot;</span> <span class="o">{</span> <span class="s2">&quot;I open...&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;print&quot;</span> <span class="o">{</span> <span class="s2">&quot;I print...&quot;</span> <span class="o">}</span>
        Default <span class="o">{</span> <span class="s2">&quot;Unknown command&quot;</span> <span class="o">}</span>
<span class="o">}</span>
Unknown <span class="nb">command</span>
</pre></div>
</div>
<p>Wildcards können ebenfalls überprüft werden</p>
<div class="highlight-sh" id="index-6"><div class="highlight"><pre><span class="nv">$text</span> <span class="o">=</span> <span class="s2">&quot;IP address: 10.10.10.10&quot;</span>
Switch -wildcard <span class="o">(</span><span class="nv">$text</span><span class="o">)</span>
<span class="o">{</span>
        <span class="s2">&quot;IP*&quot;</span> <span class="o">{</span> <span class="s2">&quot;The text begins with IP: $_&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;*.*.*.*&quot;</span> <span class="o">{</span> <span class="s2">&quot;The text contains an IP &quot;</span> +
        <span class="s2">&quot;address string pattern: $_&quot;</span> <span class="o">}</span>
        <span class="s2">&quot;*dress*&quot;</span> <span class="o">{</span> <span class="s2">&quot;The text contains the string &quot;</span> +
        <span class="s2">&quot;&#39;dress&#39; in arbitrary locations: $_&quot;</span> <span class="o">}</span>
<span class="o">}</span>
The text begins with IP: IP address: 10.10.10.10
The text contains an IP address string pattern:
IP address: 10.10.10.10
The text contains the string <span class="s1">&#39;dress&#39;</span> in arbitrary
locations: IP address: 10.10.10.10
</pre></div>
</div>
</div>
<div class="section" id="regulare-ausdrucke">
<h3>7.5.4. Reguläre Ausdrücke<a class="headerlink" href="#regulare-ausdrucke" title="Permalink zu dieser Überschrift">¶</a></h3>
</div>
<div class="section" id="switch-auf-listen">
<h3>7.5.5. switch auf Listen<a class="headerlink" href="#switch-auf-listen" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Was mach folgener Code ?</p>
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$array</span> <span class="o">=</span> 1..5

Switch <span class="o">(</span><span class="nv">$array</span><span class="o">)</span>
<span class="o">{</span>
        <span class="o">{</span><span class="nv">$_</span> % 2<span class="o">}</span> <span class="o">{</span> <span class="s2">&quot;$_ is odd.&quot;</span><span class="o">}</span>
        Default <span class="o">{</span> <span class="s2">&quot;$_ is even.&quot;</span><span class="o">}</span>
<span class="o">}</span>
1       is      odd.
2       is  even.
3       is  odd.
4       is  even.
5       is  odd.
</pre></div>
</div>
</div>
<div class="section" id="zusammenfassung">
<h3>7.5.6. Zusammenfassung<a class="headerlink" href="#zusammenfassung" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Übersetzen Sie folgenden Text:</p>
<p>Intelligent decisions are based on conditions, which in the simplest form can be reduced to plain Yes or No answers. Using the comparison operators, you can formulate such
conditions and can even combine these with the logical operators to form complex
queries.</p>
<p>The simple Yes/No answers of your conditions determine whether particular PowerShell instructions
should be carried out or not. In the simplest form, you can use the Where-Object cmdlet in the
pipeline. It functions there like a filter, allowing only those results through the pipeline that
correspond to your condition.</p>
<p>If you would like more control, or would like to execute larger code segments independently of
conditions, use the If statement, which evaluates as many different conditions as you wish and,
depending on the result, executes the allocated code. This is the typical &#8220;If-Then&#8221; scenario: if certain conditions are met, then certain code segments will be executed.</p>
<p>An alternative to the If statement is the Switch statement: using it, you can compare a fixed initial value with various possibilities. Switch is the right choice when you want to check a particular variable against many different possible values.</p>
</div>
</div>
<div class="section" id="aufgaben">
<h2>7.6. Aufgaben<a class="headerlink" href="#aufgaben" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="section" id="zahlenraten">
<h3>7.6.1. <strong>ZahlenRaten</strong><a class="headerlink" href="#zahlenraten" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Erstellen sie ein Skript, welches folgende Aufgabe erfüllt: Es muss eine Meldung anzeigen, in der der Benutzer aufgefordert wird, eine Zahl zwischen 1 und 50 einzugeben. Das Skript muss die vom Benutzer eingegebene Zahl mit einer zufällig generierten Zahl vergleichen. Wenn die Zahlen nicht übereinstimmen, muss das Skript eine Meldung anzeigen, in der angegeben wird, ob die geratene Zahl zu hoch oder zu niedrig war, und der Benutzer aufgefordert wird, noch einmal zu raten.</p>
<p>Wenn der Benutzer richtig rät, muss das Skript die Zufallszahl sowie die Anzahl der Rateversuche anzeigen. An diesem Punkt ist das Spiel beendet, das Skript muss also auch beendet werden.</p>
<p>Siehe dazu auch: <a class="reference external" href="http://msdn.microsoft.com/de-de/library/system.random_members.aspx">http://msdn.microsoft.com/de-de/library/system.random_members.aspx</a></p>
</div>
<div class="section" id="dateien-kopieren">
<h3>7.6.2. <strong>Dateien kopieren</strong><a class="headerlink" href="#dateien-kopieren" title="Permalink zu dieser Überschrift">¶</a></h3>
<p>Diese Skripts sollen folgende Aufgaben ausführen:</p>
<blockquote>
<div><ul class="simple">
<li>Durchsuchen des Ordners „C:Scripts“ und von dessen Unterordnern.</li>
<li>Durchsuchen jedes Ordners nach sämtlichen Textdateien (Dateien mit der Erweiterung „.txt“) und Prüfen des Erstellungsdatums jeder Datei.</li>
<li>Kopieren jeder .txt-Datei, die mehr als 10 Tage zuvor erstellt wurde, in den Ordner „C:Old“.</li>
<li>Ausgeben des Dateinamens (kein vollständiger Pfad, nur Dateiname) jeder kopierten Datei.</li>
<li>Ausgeben der Anzahl der kopierten Dateien.</li>
</ul>
</div></blockquote>
</div>
</div>
<div class="section" id="losungen">
<h2>7.7. Lösungen<a class="headerlink" href="#losungen" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="section" id="id1">
<h3>7.7.1. <strong>ZahlenRaten</strong><a class="headerlink" href="#id1" title="Permalink zu dieser Überschrift">¶</a></h3>
<img alt="_images/zahlenraten.png" src="_images/zahlenraten.png" />
<div class="highlight-sh"><div class="highlight"><pre><span class="nv">$guesses</span> <span class="o">=</span> 0

<span class="nv">$low</span> <span class="o">=</span> 1
<span class="nv">$high</span> <span class="o">=</span> 50

<span class="nv">$a</span> <span class="o">=</span> New-Object Random
<span class="nv">$a</span> <span class="o">=</span> <span class="nv">$a</span>.Next<span class="o">(</span><span class="nv">$low</span>,<span class="nv">$high</span><span class="o">)</span>

<span class="k">while</span> <span class="o">(</span><span class="nv">$true</span><span class="o">)</span>
<span class="o">{</span>
             <span class="nv">$guess</span> <span class="o">=</span> <span class="nb">read</span>-host <span class="s2">&quot;Enter a number between 1 and 50: &quot;</span>
             <span class="nv">$guesses</span>++

             <span class="k">if</span> <span class="o">(</span><span class="nv">$guess</span> -eq <span class="nv">$a</span><span class="o">)</span>
             <span class="o">{</span>
                     <span class="s2">&quot;Random Number: &quot;</span> + <span class="nv">$guess</span>
                     <span class="s2">&quot;Number of Guesses: &quot;</span> + <span class="nv">$guesses</span>
                     <span class="nb">break</span>
             <span class="o">}</span>
             elseif <span class="o">(</span><span class="nv">$guess</span> -gt <span class="nv">$a</span><span class="o">)</span>
             <span class="o">{</span>
                     <span class="s2">&quot;Too high&quot;</span>
             <span class="o">}</span>
             <span class="k">else</span>
             <span class="o">{</span>
                     <span class="s2">&quot;Too low&quot;</span>
             <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<h3>7.7.2. <strong>Dateien kopieren</strong><a class="headerlink" href="#id2" title="Permalink zu dieser Überschrift">¶</a></h3>
<div class="highlight-sh"><div class="highlight"><pre>foreach <span class="o">(</span><span class="nv">$i</span> in Get-ChildItem C:<span class="se">\S</span>cripts -recurse<span class="o">)</span>
<span class="o">{</span>
  <span class="k">if</span> <span class="o">((</span><span class="nv">$i</span>.CreationTime -lt <span class="o">(</span><span class="k">$(</span>Get-Date<span class="k">)</span>.AddDays<span class="o">(</span>-10<span class="o">)))</span> -and <span class="o">(</span><span class="nv">$i</span>.Extension -eq <span class="s2">&quot;.txt&quot;</span><span class="o">))</span>
  <span class="o">{</span>
      <span class="c">#Copy-Item $i.FullName C:\old</span>
      <span class="nv">$i</span>.Name
      <span class="nv">$x</span> <span class="o">=</span> <span class="nv">$x</span> + 1
  <span class="o">}</span>
<span class="o">}</span>

<span class="s2">&quot;Total Files: &quot;</span> + <span class="nv">$x</span>
</pre></div>
</div>
<span class="target" id="index-9"></span></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li class="right" >
          <a href="powershell_schleifen.html" title="8. Schleifen"
             >weiter</a> |</li>
        <li class="right" >
          <a href="powershell_arrays.html" title="6. Arrays und Hashs"
             >zurück</a> |</li>
        <li><a href="index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>